<template>
	<coreshop-page title="评论列表" mode="left">
		<view class="p-25">
			<view class="evaluate-box" v-if="state.commentsList.length > 0">
				<view class="item" v-for="item in state.commentsList" :key="item.id">
					<view class="name-box">
						<view class="avatar">
							<image class="img" :src="item.avatarImage"></image>
							<view class="name">{{ item.nickName }}</view>
						</view>
						<uv-rate :count="5" v-model="item.score" readonly activeColor="#D33123"></uv-rate>
					</view>
					<view class="content">{{ item.contentBody }}</view>
					<view class="time">{{ item.createTime }} </view>
					<view class="addon">{{ item.addon }}</view>
					<view class="sellerContent">
						商家回复：{{item.sellerContent}}
					</view>
				</view>
				<view class="no-data" v-if="state.page === state.totalPages">没有更多了</view>
			</view>
			<view v-else class="layout-empty-box">
				<coreshop-empty :mode="EmptyEnum.data" text="暂无评价"></coreshop-empty>
			</view>
		</view>
	</coreshop-page>
</template>
<script setup lang="ts">
	import { reactive } from 'vue';
	import { onLoad, onReachBottom } from '@dcloudio/uni-app';
	import type { Response, GoodsPictureType } from '@/core/models';
	import { queryGoodsComment } from '@/core/api';
	import { EmptyEnum } from '@/core/enum';

	interface QueryParams {
		id : number;
	}

	const state = reactive<{
		commentsList : Array<any>;
		totalPages : number;
		page : number;
		limit : number;
		id : number;
	}>({
		commentsList: [],
		totalPages: 1,
		page: 1,
		limit: 10,
		id: 0,
	})

	onLoad((query : QueryParams) => {
		state.id = query.id;
		getGoodsComment()
	});

	onReachBottom(() => {
		if (state.totalPages > state.page) {
			state.page++;
			getGoodsComment();
		}
	})

	/** 获取商品评价 */
	const getGoodsComment = async () => {
		const goodsComment : Response<GoodsPictureType> = await queryGoodsComment({
			id: state.id,
			limit: 10,
			page: 1
		});
		if (goodsComment.status) {
			state.totalPages = goodsComment.data.totalPages;
			state.commentsList = state.commentsList.concat(goodsComment?.data?.list);
		}
	}
</script>
<style lang="scss" scoped>
	.evaluate-box {
		.item {
			padding: 20rpx;
			background-color: #fff;
			margin-bottom: 20rpx;
			border-radius: 15rpx;

			.name-box {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.avatar {
					display: flex;
					align-items: center;

					.img {
						display: block;
						width: 50rpx;
						height: 50rpx;
						border-radius: 50%;
					}

					.name {
						font-size: 27rpx;
						margin-left: 10rpx;
					}
				}

			}

			.content {
				font-size: 27rpx;
				padding: 10rpx 0;
			}

			.time,
			.addon {
				margin-bottom: 10rpx;
				font-size: 27rpx;
				color: #aaaaaa;
			}

			.sellerContent {
				font-size: 27rpx;
				margin-top: 15rpx;
			}
		}
	}

	.no-evaluate {
		font-size: 27rpx;
		color: #aaa;
	}
</style>